package com.example.controller;


import com.example.enetity.Category;
import com.example.enetity.Result;
import com.example.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
//支持跨域
@CrossOrigin
public class CategoryController {
    @Autowired
    private CategoryService service;

    @PostMapping
    public Result add(@RequestBody @Validated Category category) {
        service.add(category);
        return  Result.success("主人，图书添加成功了");
    }

    @GetMapping
    public Result<List<Category>> getList(){
        List<Category> list = service.list();
        return Result.success("图书信息返回成功,主人" ,list);
    }
    @GetMapping("/detail")
    public Result<Category> detail(@RequestParam Integer id){
        Category category = service.getById(id);
        return Result.success("图书信息返回成功,主人" ,category);
    }

    @PutMapping("/update")
    public Result update(@RequestBody @Validated({Category.Update.class,}) Category category){
        service.updateDetail(category);
        return Result.success("主人，文章类别修改成功了");
    }

    @PutMapping("/delete")
    public Result delete(@RequestParam Integer id){
        service.removeById(id);
        return Result.success("主人，你的类别已经删除了");
    }
}
